﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using NetMicro.Domains.Strategy;

namespace NetMicro.Domains
{
    /// <summary>
    /// 领域层注入 
    /// </summary>
    public static class ServiceCollectionExtensions
    {

        /// <summary>
        /// 注册仓储上下文
        /// </summary>
        /// <param name="services">依赖注入服务容器</param>
        /// <returns></returns>
        public static IServiceCollection AddContext<TContext>(this IServiceCollection services)
            where TContext : DbContext
        {
            services.AddSingleton<TContext>();
            services.AddSubDatabaseWithTable();
            return services;
        }
        /// <summary>
        /// 注册仓储上下文
        /// </summary>
        /// <typeparam name="TContext"></typeparam>
        /// <param name="services"></param>
        /// <param name="action"></param>
        /// <returns></returns>
        public static IServiceCollection AddContext<TContext>(this IServiceCollection services, Action<DbOptions> action)
            where TContext : DbContext
        {
            services.Configure(action);
            services.AddSingleton<TContext>();
            services.AddSubDatabaseWithTable();
            return services;
        }
        /// <summary>
        /// 注册仓储上下文
        /// </summary>
        /// <typeparam name="TContext"></typeparam>
        /// <param name="services"></param>
        /// <param name="action"></param>
        /// <returns></returns>
        public static IServiceCollection AddContexts<TContext>(this IServiceCollection services, Action<List<DbOptions>> action)
            where TContext : DbContext
        {
            services.Configure(action);
            services.AddSingleton<TContext>();
            services.AddSubDatabaseWithTable();
            return services;
        }

        /// <summary>
        /// 注册仓储上下文
        /// </summary>
        /// <typeparam name="TContext"></typeparam>
        /// <param name="services"></param>
        /// <param name="action"></param>
        /// <returns></returns>
        public static IServiceCollection AddContexts<TContext, TContext1>(this IServiceCollection services, Action<List<DbOptions>> action)
            where TContext : DbContext
            where TContext1 : DbContext
        {
            services.Configure(action);
            services.AddSingleton<TContext>();
            services.AddSingleton<TContext1>();
            services.AddSubDatabaseWithTable();
            return services;
        }

        /// <summary>
        /// 注册仓储上下文
        /// </summary>
        /// <typeparam name="TContext"></typeparam>
        /// <param name="services"></param>
        /// <param name="action"></param>
        /// <returns></returns>
        public static IServiceCollection AddContexts<TContext, TContext1, TContext2>(this IServiceCollection services, Action<List<DbOptions>> action)
            where TContext : DbContext
            where TContext1 : DbContext
            where TContext2 : DbContext
        {
            services.Configure(action);
            services.AddSingleton<TContext>();
            services.AddSingleton<TContext1>();
            services.AddSingleton<TContext2>();
            services.AddSubDatabaseWithTable();
            return services;
        }

        /// <summary>
        /// 注册仓储上下文
        /// </summary>
        /// <typeparam name="TContext"></typeparam>
        /// <param name="services"></param>
        /// <param name="action"></param>
        /// <returns></returns>
        public static IServiceCollection AddContexts<TContext, TContext1, TContext2, TContext3>(this IServiceCollection services, Action<List<DbOptions>> action)
            where TContext : DbContext
            where TContext1 : DbContext
            where TContext2 : DbContext
            where TContext3 : DbContext
        {
            services.Configure(action);
            services.AddSingleton<TContext>();
            services.AddSingleton<TContext1>();
            services.AddSingleton<TContext2>();
            services.AddSingleton<TContext3>();
            services.AddSubDatabaseWithTable();
            return services;
        }

        /// <summary>
        /// 注册仓储上下文
        /// </summary>
        /// <typeparam name="TContext"></typeparam>
        /// <param name="services"></param>
        /// <param name="action"></param>
        /// <returns></returns>
        public static IServiceCollection AddContexts<TContext, TContext1, TContext2, TContext3, TContext4>(this IServiceCollection services, Action<List<DbOptions>> action)
            where TContext : DbContext
            where TContext1 : DbContext
            where TContext2 : DbContext
            where TContext3 : DbContext
            where TContext4 : DbContext
        {
            services.Configure(action);
            services.AddSingleton<TContext>();
            services.AddSingleton<TContext1>();
            services.AddSingleton<TContext2>();
            services.AddSingleton<TContext3>();
            services.AddSingleton<TContext4>();
            services.AddSubDatabaseWithTable();
            return services;
        }

        /// <summary>
        /// 注册分库分表默认策略
        /// </summary>
        /// <param name="services"></param>
        /// <returns></returns>
        private static IServiceCollection AddSubDatabaseWithTable(this IServiceCollection services)
        {
            services.AddSingleton<ISubDatabaseDbConfig, DefaultSubDatabaseDbConfig>();
            services.AddSingleton<ISqlTableFactory, DefaultTableFactory>();
            return services;
        }
    }
}
